package lol_stream_manager;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utility {
    
    public static long rtmpDumpTimeout = 15000;
    public static long rtmpDumpMaxSize = 1500000;
    public static long rtmpDumpMaxSec = 10;

    public static String getWebPage( String sUrl ){
        StringBuilder page = new StringBuilder();
        try{
            URL url = new URL( sUrl.replace(" ", "%20"));
            InputStream is = url.openStream();
            DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
            
            String line;
            while((line = dis.readLine()) != null)
                page.append(line).append("\n");
            
        } catch( Exception e) {}
        
        return page.toString();
    }
    
    public static String matchRegex(String string, String regex){
        Pattern p = Pattern.compile( regex );
        Matcher m = p.matcher( string );
        if( m.find() )
            return m.group(1);
        return null;
    }
    
    public static int executeCommandLine(final String commandLine,
            final boolean printOutput,
            final boolean printError,
            final long timeout)
            throws IOException, InterruptedException, TimeoutException {
        System.out.println("Started: " + commandLine);
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(commandLine);
        
        Worker worker = new Worker(process);
        worker.start();
        try {
            worker.join(timeout);
            System.out.println("Ended");
            if (worker.exit != null) {
                return worker.exit;
            } else {
                worker.terminate();
                return -1;
            }
        } catch (InterruptedException ex) {
            worker.interrupt();
            Thread.currentThread().interrupt();
            throw ex;
        } finally {
            process.destroy();
        }
    }

    private static class Worker extends Thread {

        private final Process process;
        private Integer exit;

        private Worker(Process process) {
            this.process = process;
        }

        @Override
        public void run() {
            try {
                exit = process.waitFor();
            } catch (InterruptedException ignore) {}
        }
        
        public void terminate(){
            process.destroy();
        }
    }
    
    static final int PROGRESSBAR_LENGTH = 20;

    public static void drawProgressBar(int numerator, int denominator) {
        int percent = (int) (((double) numerator / (double) denominator) * 100);

        String bar = "[";
        int lines = round((PROGRESSBAR_LENGTH * numerator) / denominator);
        int blanks = PROGRESSBAR_LENGTH - lines;

        for (int i = 0; i < lines; i++) {
            bar += "X";
        }

        for (int i = 0; i < blanks; i++) {
            bar += " ";
        }

        bar += "] " + percent + "%";

        System.out.print(bar + "\r");
    }

    private static int round(double dbl) {
        int noDecimal = (int) dbl;
        double decimal = dbl - noDecimal;

        if (decimal >= 0.5) {
            return noDecimal + 1;
        } else {
            return noDecimal;
        }
    }
    
    public static class chronometer {
        public long start;
        
        public chronometer(){
            this.start = System.currentTimeMillis();
        }
        
        public long getChrono(){
            return System.currentTimeMillis() - start;
        }
        
        @Override
        public String toString(){
            String t = "";
            long tmp = getChrono();
                // Millis
            long s = tmp % 1000;
            t += s;
            if( s<10 ) t = "0" + t;
            if( s<100 ) t = "0" + t;
            tmp /= 1000;
            s = tmp % 60;
                // Seconds
            t = s + "." + t;
            if( s<10 ) t = "0" + t;
            tmp /= 60;
            if( tmp == 0 )
                return t;
            s = tmp % 60;
                // Minutes
            t = s + ":" + t;
            if( s<10 ) t = "0" + t;
            tmp /= 60;
            if( tmp == 0 )
                return t;
                // Hours
            s = tmp % 60;
            t = s + ":" + t;
            if( s<10 ) t = "0" + t;
            
            return t;
        }
    }
    
}
